// pages/product/view.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    product:{},
    images: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({id:options.id});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.loadProduct();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadProduct();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onPreview: function(e){
    console.log(e);
    wx.previewImage({
      urls: this.data.images,
      current: e.currentTarget.dataset.image
    })
  },
  loadProduct: function(){
    var self = this;
    app.httpGet("/index.php?r=v1/product/index&id=" + this.data.id, {success:function(res){
      self.setData(
        { product: res.data }
      );
      console.log(res.data);
      let images = [];
      res.data.images.forEach(function(v,i){
        console.log(v);
        images.push(v.url);
      });
      self.setData({images:images});
    }});
  },
  updateProduct: function () {
    this.setData({
      product: this.data.product
    });
  },
  onLoveClicked: function (e) {
    var self = this;
    var productId = e.currentTarget.dataset.id;
    var item = self.data.product;

    if (item.is_favorite == true) {
      app.httpDelete('/index.php?r=v1/favorite/index&id=' + productId, {
        success: function (res) {
          console.log(res);
          item.is_favorite = res.data.is_favorite;
          self.updateProduct();
          wx.showToast({
            title: '已取消',
            icon: 'none'
          });
        },
      });
    }
    else {
      app.httpPost('/index.php?r=v1/favorite/index', {
        success: function (res) {
          console.log(res);
          item.is_favorite = res.data.is_favorite;
          self.updateProduct();
          wx.showToast({
            title: '已关注',
          });
        },
      },
        { product_id: productId }
      );
    }
  },
  onBidClicked: function (e) {
    var self = this;
    var productId = e.currentTarget.dataset.id;
    var item = self.data.product;

    if (item.next_bid >= item.price) {
      self.onBuyClicked(e);
      return;
    }

    if (item.can_bid) {
      wx.showModal({
        title: '确认要出价吗？',
        confirmText: '出价',
        content: item.next_bid + '',
        success: function (res) {
          if (res.confirm) {
            console.log('bid');
            app.httpPost('/index.php?r=v1/bid/index', {
              success: function (res) {
                console.log(res);
                if (res.code == 200) {
                  item.bid_total = res.data.bid_total;
                  item.next_bid = res.data.next_bid;
                  self.updateProduct();
                }
                else {
                  wx.showToast({
                    title: res.msg,
                  })
                }
              }
            }, {
                product_id: item.id,
                price: item.next_bid
              });
          }
        }
      })
    }
    else {
      wx.showToast({
        title: '尚未开售',
        icon: 'none'
      })
    }
  },
  onBuyClicked: function(e)
  {
    var productId = e.currentTarget.dataset.id;
    var item = this.getProductItem(productId);
    var self = this;

    if (item.can_bid) {
      wx.showModal({
        title: '确认要出价吗？',
        confirmText: '出价',
        content: item.price + '',
        success: function (res) {
          if (res.confirm) {
            console.log('bid');
            app.httpPost('/index.php?r=v1/bid/buy', {
              success: function (res) {
                console.log(res);
                if (res.code == 200) {
                  item.bid_total = res.data.bid_total;
                  item.next_bid = res.data.next_bid;
                  self.updateProducts();
                  if (res.data.order_id) {
                    var url = "/pages/my/confirm?id=" + res.data.order_id;
                    wx.navigateTo({
                      url: url,
                    })
                  }
                }
                else if (res.code == 100800) {
                  wx.showModal({
                    title: '出价错误',
                    content: '你需要缴纳保证金后才能参与拍卖,请先缴纳保证金',
                    confirmText: '缴纳保证金',
                    success: function (res) {
                      if (res.confirm) {
                        action.payDeposit(self);
                      }
                    }
                  })
                }
                else {
                  wx.showToast({
                    title: res.msg,
                  })
                }
              }
            }, {
                product_id: item.id,
                price: item.next_bid
              });
          }
        }
      })
    }
    else {
      wx.showToast({
        title: '尚未开始',
        icon: 'none'
      })
    }
  },
  goAuthorDetail: function(e)
  {
    var self = this;
    var authorId = e.currentTarget.dataset.id;
    console.log(authorId);
    wx.navigateTo({
      url: '/pages/author/view?id='+authorId,
    })
  }
})